<template>
    <div class="app-container">
      <el-form ref="form" :model="form" label-width="80px" :rules="rules"  v-loading="formLoading" >

				<el-form-item label="原密码" prop="name">
					<el-input type="password" v-model="form.oldPassword"></el-input>
				</el-form-item>
				<el-form-item label="新密码" prop="password" >
					<el-input type="password" v-model="form.password"></el-input>
				</el-form-item>
				<el-form-item   label="确认密码" prop="repassword" >
					<el-input type="password" v-model="form.repassword"></el-input>
				</el-form-item>

				<el-form-item  >
					<el-button type="primary" @click="onSubmit('form')">修改</el-button>
				</el-form-item>
			</el-form>


    </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import { myPost } from '@/utils/myRequest'

const urlUserResetPassword = '/guard/rbac/guardUser/resetPassword'
export default {
  data() {

    let reg = /(?!^(\d+|[a-zA-Z]+|[~!@#$%^&*?]+)$)^[\w~!@#$%^&*?]{6,12}$/
        var validateNewPwd = (rule, value, callback) => {
            if (!reg.test(value)) {
                callback(new Error('密码应是6-12位数字、字母或字符！'))
            } /*else if (this.form.oldPasswd === value) {
                callback(new Error('新密码与旧密码不可一致！'))
            } */else {
                callback()
            }
        }
        var validateComfirmPwd = (rule, value, callback) => {
            if (!reg.test(value)) {
                callback(new Error('密码应是6-12位数字、字母或字符！'))
            } else if (this.form.password !== value) {
                callback(new Error('确认密码与新密码不一致！'))
            } else {
                callback()
            }
        }

    return {
      formLoading:false,
      form:{
        oldPassword:'',
        password:'',
        repassword:'',
      },
      rules: {
        password:[
            { required: true, message: '请输入新密码', trigger: 'blur' },
            { validator: validateNewPwd, trigger: 'blur' }
        ],
        repassword: [
            { required: true, message: '请输入确认密码', trigger: 'blur' },
            { validator: validateComfirmPwd, trigger: 'blur' }
        ]
      },
    }
  },

  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.formLoading = true;
          myPost(urlUserResetPassword,this.form).then(res=>{
            this.formLoading = false;
            if(res.status){
              this.$message.success(res.msg);
              Object.assign(this.form,this.$options.data().form)
            }
          })
        } else {
          this.$message.error('验证失败！');
          return false;
        }
      });
    }
  },

}
</script>

<style>

</style>
